package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Slf4j
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /*
     * 接单
     * */
    @PutMapping("/confirm")
    public Result accept(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("商家接单，订单号：{} ", ordersConfirmDTO.getId());
        orderService.accept(ordersConfirmDTO.getId());
        return Result.success();
    }

    /*
    * 派送订单
    * */

    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable("id") Long orderId) {
        log.info("派送订单，订单号：{} ", orderId);
        orderService.delivery(orderId);
        return Result.success();
    }

    /*
    * 商家取消订单
    */
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("商家取消订单，订单号：{} ", ordersCancelDTO.getId());
        orderService.cancelByBusiness(ordersCancelDTO);
        return Result.success();
    }

    /*
    * 商家拒绝订单
    * */
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("商家拒绝订单，订单号：{} ", ordersRejectionDTO.getId());
        orderService.rejectionByBusiness(ordersRejectionDTO);
        return Result.success();
    }

    /*
    * 商家完成订单
    * */
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable("id") Long orderId) {
        log.info("商家完成订单，订单号：{} ", orderId);
        orderService.complete(orderId);
        return Result.success();
    }

    /*
    * 统计各个状态订单数量
    * */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics() {
        log.info("统计各个状态订单数量");
        return Result.success(orderService.statistics());
    }

    /*
    *
    * 根据id查询订单详情
    * */
    @GetMapping("/details/{id}")
    public Result<OrderVO> getDetail(@PathVariable("id") Long orderId){
        log.info("查询订单详情：订单id ：{}", orderId);
        return Result.success(orderService.getOrderDetail(orderId));
    }

    /*
    * 订单搜索(本质就是分页查询)
    * */
    @GetMapping("/conditionSearch")
    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("订单搜索：{}", ordersPageQueryDTO);
        return Result.success(orderService.pageByAdmin(ordersPageQueryDTO));
    }


}
